package c.c.b.a.a;

import c.b.a.a.a;
import java.io.EOFException;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

/* compiled from: InflaterSource */
public final class m implements w {
    public final g a;
    public final Inflater b;
    public int c;
    public boolean d;

    public m(g gVar, Inflater inflater) {
        this.a = gVar;
        this.b = inflater;
    }

    public x a() {
        return this.a.a();
    }

    public long b(e eVar, long j) throws IOException {
        int i = (j > 0 ? 1 : (j == 0 ? 0 : -1));
        if (i < 0) {
            throw new IllegalArgumentException(a.B("byteCount < 0: ", j));
        } else if (this.d) {
            throw new IllegalStateException("closed");
        } else if (i == 0) {
            return 0;
        } else {
            s sVar;
            while (true) {
                Object obj = null;
                if (this.b.needsInput()) {
                    n();
                    if (this.b.getRemaining() != 0) {
                        throw new IllegalStateException("?");
                    } else if (this.a.e()) {
                        obj = 1;
                    } else {
                        sVar = this.a.c().a;
                        int i2 = sVar.c;
                        int i3 = sVar.b;
                        i2 -= i3;
                        this.c = i2;
                        this.b.setInput(sVar.a, i3, i2);
                    }
                }
                try {
                    sVar = eVar.w0(1);
                    i = this.b.inflate(sVar.a, sVar.c, (int) Math.min(j, (long) (8192 - sVar.c)));
                    if (i > 0) {
                        sVar.c += i;
                        long j2 = (long) i;
                        eVar.b += j2;
                        return j2;
                    } else if (this.b.finished()) {
                        break;
                    } else if (this.b.needsDictionary()) {
                        break;
                    } else if (obj != null) {
                        throw new EOFException("source exhausted prematurely");
                    }
                } catch (DataFormatException e) {
                    throw new IOException(e);
                }
            }
            n();
            if (sVar.b == sVar.c) {
                eVar.a = sVar.d();
                t.b(sVar);
            }
            return -1;
        }
    }

    public void close() throws IOException {
        if (!this.d) {
            this.b.end();
            this.d = true;
            this.a.close();
        }
    }

    public final void n() throws IOException {
        int i = this.c;
        if (i != 0) {
            i -= this.b.getRemaining();
            this.c -= i;
            this.a.p((long) i);
        }
    }
}
